package com.o2o.cleaning.month.platform.ebusiness_plat.jumei_2019_7

import com.alibaba.fastjson.{JSON, JSONObject}
import com.o2o.utils.Iargs
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/6/5 16:23
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object InsertESData1 {
  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name","O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", Iargs.OBSACCESS)
    sc.hadoopConfiguration.set("fs.s3a.secret.key", Iargs.OBSSECRET)
    sc.hadoopConfiguration.set("fs.s3a.endpoint", Iargs.OBSENDPOINT)
    sc.setLogLevel("WARN")

    import org.elasticsearch.spark._


    val values: RDD[String] = sc.esJsonRDD("2020_taobao/taobao_2020_7",
      """
        |{
        |  "query": {
        |    "bool": {
        |      "must": [
        |        {"match_phrase": {
        |          "categoryId": "1205"
        |        }},
        |        {"match_phrase": {
        |          "thirdCategoryId": "100999999"
        |        }}
        |      ]
        |    }
        |  }
        |}
      """.stripMargin).values

    spark.read.json(values).toJSON.rdd.map(lines=>{
      val nObject: JSONObject = JSON.parseObject(lines)

      nObject.put("firstCategoryId","10015")
      nObject.put("secondCategoryId","1001501")
      nObject.put("thirdCategoryId","100150103")
      nObject.put("fourthCategoryId","10015010399")

      nObject
    }).saveToEs("2020_taobao/taobao_2020_7",Map("es.mapping.id"->"good_id"))

  sc.stop()
}
}
